/*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Author:Nicolas Longuet Marx
Date: May 5 2021
Last modified: August 19 2023
Object: Create figures of evolution of satisfaction levels
Databases in input: A_experiment.dta and B_panel.dta
Databases in output: None

Figures generated in this script:

- Figure F.1. Aggregate trends in the main variables of interest
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ****/




**# Figure 

set scheme s2mono
grstyle init
grstyle set plain, horizontal grid dotted

use "1_data/A_experiment.dta", replace

gen above_50 = fifties +sixties +seventies
la var college "College degree"

gen income_above_median = income3quartile + income4quartile

/* Health */
reg eval_sant healthc (c.healthc )#(i.college i.above_50 i.female i.affiliated_gov i.noreligion i.income_above_median )  i.college i.above_50 i.female i.affiliated_gov i.noreligion i.income_above_median , robust

capture drop coef_health lb_health ub_health
gen coef_health = . 
gen lb_health =  . 
gen ub_health =  . 

capture drop type
gen type = _n 

lincom 1.noreligion#c.healthc 
qui replace coef_health = `r(estimate)' if type == 1
qui replace lb_health = `r(lb)' if type == 1
qui replace ub_health = `r(ub)' if type == 1

lincom 1.above_50#c.healthc 
qui replace coef_health = `r(estimate)' if type == 2
qui replace lb_health = `r(lb)' if type == 2
qui replace ub_health = `r(ub)' if type == 2

lincom 1.female#c.healthc 
qui replace coef_health = `r(estimate)' if type == 3
qui replace lb_health = `r(lb)' if type == 3
qui replace ub_health = `r(ub)' if type == 3

lincom 1.affiliated_gov#c.healthc 
qui replace coef_health = `r(estimate)' if type == 4
qui replace lb_health = `r(lb)' if type == 4
qui replace ub_health = `r(ub)' if type == 4

lincom 1.income_above_median#c.healthc 
qui replace coef_health = `r(estimate)' if type == 5
qui replace lb_health= `r(lb)' if type == 5
qui replace ub_health = `r(ub)' if type == 5

lincom 1.college#c.healthc 
qui replace coef_health = `r(estimate)' if type == 6
qui replace lb_health = `r(lb)' if type == 6
qui replace ub_health = `r(ub)' if type == 6


/* Econ */
reg eval_eco econc (c.econc )#(i.college i.above_50 i.female i.affiliated_gov i.noreligion i.income_above_median )  i.college i.above_50 i.female i.affiliated_gov i.noreligion i.income_above_median , robust

capture drop coef_eco lb_eco ub_eco
gen coef_eco = . 
gen lb_eco =  . 
gen ub_eco =  . 
capture drop type_eco
gen type_eco = _n

lincom 1.noreligion#c.econc 
qui replace coef_eco = `r(estimate)' if type_eco == 1
qui replace lb_eco = `r(lb)' if type_eco == 1
qui replace ub_eco = `r(ub)' if type_eco == 1

lincom 1.above_50#c.econc 
qui replace coef_eco = `r(estimate)' if type_eco == 2
qui replace lb_eco = `r(lb)' if type_eco == 2
qui replace ub_eco = `r(ub)' if type_eco == 2

lincom 1.female#c.econc 
qui replace coef_eco = `r(estimate)' if type_eco == 3
qui replace lb_eco = `r(lb)' if type_eco == 3
qui replace ub_eco = `r(ub)' if type_eco == 3

lincom 1.affiliated_gov#c.econc 
qui replace coef_eco = `r(estimate)' if type_eco == 4
qui replace lb_eco = `r(lb)' if type_eco == 4
qui replace ub_eco = `r(ub)' if type_eco == 4

lincom 1.income_above_median#c.econc 
qui replace coef_eco = `r(estimate)' if type_eco == 5
qui replace lb_eco = `r(lb)' if type_eco == 5
qui replace ub_eco = `r(ub)' if type_eco == 5

lincom 1.college#c.econc 
qui replace coef_eco = `r(estimate)' if type_eco == 6
qui replace lb_eco = `r(lb)' if type_eco == 6
qui replace ub_eco = `r(ub)' if type_eco == 6

replace type_eco = type_eco-0.2


/* Satisfaction with the head of government */
reg satis_head treatc (c.treatc )#(i.college i.above_50 i.female i.affiliated_gov i.noreligion i.income_above_median )  i.college i.above_50 i.female i.affiliated_gov i.noreligion i.income_above_median , robust

capture drop coef lb ub
gen coef = . 
gen lb =  . 
gen ub =  . 
capture drop type_general
gen type_general = _n 

lincom 1.noreligion#c.treatc 
qui replace coef = `r(estimate)' if type_general == 1
qui replace lb = `r(lb)' if type_general == 1
qui replace ub = `r(ub)' if type_general == 1

lincom 1.above_50#c.treatc 
qui replace coef = `r(estimate)' if type_general == 2
qui replace lb = `r(lb)' if type_general == 2
qui replace ub = `r(ub)' if type_general == 2

lincom 1.female#c.treatc 
qui replace coef = `r(estimate)' if type_general == 3
qui replace lb = `r(lb)' if type_general == 3
qui replace ub = `r(ub)' if type_general == 3

lincom 1.affiliated_gov#c.treatc 
qui replace coef = `r(estimate)' if type_general == 4
qui replace lb = `r(lb)' if type_general == 4
qui replace ub = `r(ub)' if type_general == 4

lincom 1.income_above_median#c.treatc 
qui replace coef = `r(estimate)' if type_general == 5
qui replace lb = `r(lb)' if type_general == 5
qui replace ub = `r(ub)' if type_general == 5

lincom 1.college#c.treatc 
qui replace coef = `r(estimate)' if type_general == 6
qui replace lb = `r(lb)' if type_general == 6
qui replace ub = `r(ub)' if type_general == 6

replace type_general = type_general-0.4


preserve 
drop if coef ==. 
tw (scatter type coef_health, color(green) m(dh)) (rcap  lb_health ub_health type, horizontal color(green%50)) ///
(scatter type_eco coef_eco, color(orange_red) m(sh)) (rcap  lb_eco ub_eco type_eco, horizontal color(orange_red%50)) ///
(scatter type_general coef, color(midblue) m(Oh)) (rcap  lb ub type_general, horizontal color(midblue%50))  ,  legend( col(3)   size(small) order(1 "Health" 3 "Economic" 5 "Head of Goverment")) yla(1 "No religion" 2 "Above 50 y.o." 3 "Female" 4 "Affiliated with gov" 5 "Income above median" 6 "College graduate" , nogrid) ytitle("") xtitle("Coefficient on interaction") xline(0) xla(,nogrid)
graph export "3_output/2_OA/Figures/FigureE1.eps", replace 
graph export "3_output/2_OA/Figures/FigureE1.pdf", as(pdf)  replace

restore












/* Graph with all countries together */	
set scheme plotplainblind

use "1_data/B_panel.dta", replace


/* we give equal weights to each country */
gen weights = .
levelsof country, local(levels)
foreach l of local levels {
	forval n = 1/4 {
		count if country == "`l'" & wave == `n'
		replace weights = 1/r(N) if country == "`l'"  & wave == `n'
	}
}
foreach var of varlist satis_head satis_dem {
	reg `var' i.wave [aw = weights]

	gen coef_`var' = . 
	gen cimin_`var' = .
	gen cimax_`var' = .


	levelsof wave, local(l_wave) 
	foreach l of local l_wave {
		qui count if `var' !=. & wave == `l' 
		if r(N)!=0 {
			lincom _cons+`l'.wave
			replace coef_`var' = r(estimate) if wave == `l'
			replace cimin_`var' = r(lb) if wave == `l' 
			replace cimax_`var' = r(ub) if wave == `l'
		}
	}
}		

keep coef* cimin* cimax* wave	
duplicates drop



graph twoway (rcap cimax_satis_dem cimin_satis_dem  wave, lp(dash) lcolor("0 100 200 %85") )  (connected  coef_satis_dem  wave, lp(longdash) msymbol(dh) msize(6pt) color("0 100 200") )  (rcap cimax_satis_head cimin_satis_head  wave,  lcolor("200 50 70 %85") lp(dash))  (connected  coef_satis_head  wave, lp(longdash) msymbol(S) msize(4pt) color("200 50 50")  )   ,  legend(order(2 "Satisfaction with" "democracy" 4 "Satisfaction with" "the head of government" ) on ring(0) pos(1) col(1) region(lstyle(none))  ) ///
ytitle("Satisfaction level")  ylab(, nogrid) ///
xlabel( 1 "March 26 2020" 2 "April 15 2020" 3 "June 16 2020" 4 "July 17 2020" , nogrid ) xtitle("Date") xscale(range(1 4.2))
graph export "3_output/2_OA/Figures/FigureF1.eps", replace 
graph export "3_output/2_OA/Figures/FigureF1.pdf", as(pdf) replace



**** back of the enveloppe computation 
qui su coef_satis_dem if wave == 4
local satis_dem_end = r(mean)
qui su coef_satis_dem if wave == 2
local satis_dem_beg = r(mean)

qui su coef_satis_head if wave == 4
local satis_head_end = r(mean)
qui su coef_satis_head if wave == 2
local satis_head_beg = r(mean)

*** using most prefered estimate: 0.460 

local dif_dem = `satis_dem_end'-`satis_dem_beg'
local dif_swg = `satis_head_end'-`satis_head_beg'

local explained = `dif_swg'*0.460
local share_explained = `explained'/`dif_dem'
di "`explained'"
di "`share_explained'"
